package com.yanqu.road.web.controller.usercenter.newadmin;

import com.alibaba.fastjson.JSONObject;
import com.yanqu.road.dao.impl.admin.AdminCommonDaoImpl;
import com.yanqu.road.entity.admin.AdminResult;
import com.yanqu.road.entity.union.activitygroup.UnionActivityGroupRule;
import com.yanqu.road.utils.string.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;

/**
 * 39 跨服活动定制分组
 */
@RestController
@RequestMapping("/usercenter/admin")
public class CenterCrossActivityGroupController {

    /**
     * 41.1 新增
     */
    @ResponseBody
    @RequestMapping(value = "addActivityIsolate", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    public String addIsolate(HttpServletRequest request, @RequestBody JSONObject paramObject){

        if(!paramObject.containsKey("type")){
            return AdminResult.failMsg("参数不足").toJsonString();
        }
        if(!paramObject.containsKey("childType")){
            return AdminResult.failMsg("参数不足").toJsonString();
        }

        UnionActivityGroupRule rule = new UnionActivityGroupRule();
        rule.setActivityType(paramObject.getIntValue("type"));
        rule.setChildType(paramObject.getIntValue("childType"));
        rule.setUnionUid1(paramObject.getString("combinationServerIds"));
        rule.setUnionUid2(paramObject.getString("isolateServerIds"));
        String remark = paramObject.getString("remark");
        if(!StringUtils.isNullOrEmpty(rule.getRemark())){
            rule.setRemark(remark);
        }else {
            rule.setRemark("");
        }

        ArrayList<UnionActivityGroupRule> list = new ArrayList<>();
        list.add(rule);

        boolean success = new AdminCommonDaoImpl().batchInsertUnionActivityGroupRule(list);
        if(!success){
            AdminResult.failMsg("导入失败");
        }

        return AdminResult.defaultInstance().toJsonString();
    }
}
